// TWExtAdfilter.cpp : Implementation of CTWExtAdfilter
#include "stdafx.h"
#include "ExtAdfilter.h"
#include "TWExtAdfilter.h"
#include <ExDispid.h>
#include <shlguid.h>
#include <comdef.h>
#include <wininet.h>
#include <assert.h>
#include "TWExtension_i.c"
#include "..\ExtFuncHelper.h"

/////////////////////////////////////////////////////////////////////////////
// CTWExtAdfilter

extern UINT			_nDllRefCount;
extern LPVOID		_lpExtItem_Demo;
extern HINSTANCE	_hInstanceRes;


CExtAdfilterObject::CExtAdfilterObject( )
{
	m_ObjRefCount = 1;
	_nDllRefCount++;

	m_pCP = NULL;
	m_pCPC = NULL;
	m_dwCookie = 0;

	m_dwActive = 0;

	m_hBrowserWnd = NULL;
	memset( m_szUrl, 0, sizeof(m_szUrl) );

	m_pWebBrowser = NULL;
	m_pBrowserHost = NULL;
}

CExtAdfilterObject::~CExtAdfilterObject()
{
	_nDllRefCount--;
}

///////////////////////////////////////////////////////////////
// IUnknown
STDMETHODIMP CExtAdfilterObject::QueryInterface(REFIID riid, LPVOID *ppReturn)
{
	*ppReturn = NULL;

	//IUnknown
	if( IsEqualIID( riid, IID_IUnknown ) )
	{
		*ppReturn = this;
	}
	else if( IsEqualIID( riid, IID_IDispatch ) )
	{
		*ppReturn = (IDispatch*)this;
	}
	//IObjectWithSite
	else if( IsEqualIID( riid, IID_IObjectWithSite ) )
	{
		*ppReturn = (IObjectWithSite*)this;
	}
	else if( IsEqualIID( riid, IID_ITWExtBrowserEvent ) )
	{
		*ppReturn = (ITWExtBrowserEvent*)this;
	}
	else if( IsEqualIID( riid, IID_ITWExtMouseMessage ) )
	{
		*ppReturn = (ITWExtMouseMessage*)this;
	}
	else if( IsEqualIID( riid, IID_ITWExtMsg ) )
	{
		*ppReturn = (ITWExtMsg*)this;
	}

	if(*ppReturn)
	{
		(*(LPUNKNOWN*)ppReturn)->AddRef();
		return S_OK;
	}

	return E_NOINTERFACE;
}

STDMETHODIMP_(DWORD) CExtAdfilterObject::AddRef()
{
	return m_ObjRefCount++;
}

STDMETHODIMP_(DWORD) CExtAdfilterObject::Release()
{
	m_ObjRefCount--;

	if( m_ObjRefCount == 0 )
	{
		delete this;
		return 0;
	}
   
	return m_ObjRefCount;
}

///////////////////////////////////////////////////////////////
// IObjectWithSite
STDMETHODIMP CExtAdfilterObject::SetSite(IUnknown* punkSite)
{
	if( punkSite )
	{
		if( m_pWebBrowser )
		{
			m_pWebBrowser->Release();
			m_pWebBrowser = NULL;
		}
		if( m_pBrowserHost )
		{
			m_pBrowserHost->Release();
			m_pBrowserHost = NULL;
		}

		punkSite->QueryInterface( IID_IWebBrowser, (void**)&m_pWebBrowser );
		punkSite->QueryInterface( IID_ITWExtBrowserHost, (void**)&m_pBrowserHost );

		if( m_pWebBrowser == NULL )
		{
			IServiceProviderPtr pSP = punkSite;

			if( pSP )
				pSP->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (void**)&m_pWebBrowser );
		}

		if( m_pWebBrowser )
		{
			HWND hWnd = NULL;
			IServiceProvider* pServiceProvider = NULL;
			if (SUCCEEDED(m_pWebBrowser->QueryInterface( IID_IServiceProvider, (void**)&pServiceProvider)) )
			{
				HWND hWnd = NULL;
				IOleWindow* pWindow = NULL;
				if (SUCCEEDED(pServiceProvider->QueryService( SID_SShellBrowser, IID_IOleWindow, (void**)&pWindow)) )
				{
					pWindow->GetWindow(&m_hBrowserWnd);
					pWindow->Release();
				}
				pServiceProvider->Release();
			}
		}
	}
	else
	{
		if( m_pWebBrowser )
		{
			m_pWebBrowser->Release();
			m_pWebBrowser = NULL;
		}
		if( m_pBrowserHost )
		{
			m_pBrowserHost->Release();
			m_pBrowserHost = NULL;
		}
	}
	return S_OK;
}

STDMETHODIMP CExtAdfilterObject::GetSite(REFIID riid, LPVOID *ppvReturn)
{
	assert( 0 );
	*ppvReturn = NULL;
	return E_FAIL;
}

BOOL CExtAdfilterObject::IsTopWebBrowser(IDispatch *pDisp)
{
	if( pDisp )
	{
		IWebBrowser2Ptr pWeb = NULL;
		pWeb = pDisp;
		if( pWeb )
		{
			if(pWeb == m_pWebBrowser)
			{
				return TRUE;
			}
		}
	}
	return FALSE;
}

// ITWExtBrowserEvent
HRESULT CExtAdfilterObject::BrowserEventInvoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
											   VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr, 
											   BOOL* pbHandled, DWORD dwBrowserState )
{
	if( m_pWebBrowser )
	{
		if( DISPID_BEFORENAVIGATE2 == dispIdMember )
		{
			if( IsTopWebBrowser( pDispParams->rgvarg[6].pdispVal ) )
			{
			//	GetProfileSection()
				_sntprintf( m_szUrl, SIZEOF(m_szUrl)-1, _T("%s"), OLE2T(pDispParams->rgvarg[5].pvarVal->bstrVal) );
				LPTSTR urlVal = OLE2T(pDispParams->rgvarg[5].pvarVal->bstrVal);
				LPTSTR urlValLwr = wcslwr( urlVal );
				if( !wcsnicmp( urlValLwr, L"http://", 7 ) )
				{
				//	SendExtMessage( WM_EXTDEMO_SETICON, 0, 0 );
					WCHAR *suffix = urlVal + 7;
					WCHAR *substr  = wcstok( suffix, L"/" );

					WCHAR cnt[ 100 ];
					GetProfileString( L"ext_demo", substr, L"0", cnt, MAX_PATH );
					int ret = _wtoi( cnt ) + 1;
					wsprintf( cnt, L"%d", ret );
					//MessageBox( NULL, baidu_cnt, baidu_cnt, MB_OK );
					WriteProfileString( _T("ext_demo") , substr, cnt );
				} else if ( !wcsnicmp( urlValLwr, L"https://", 8 ) ) {
					WCHAR *suffix = urlVal + 8;
					WCHAR *substr  = wcstok( suffix, L"/" );
					
					WCHAR cnt[ 100 ];
					GetProfileString( L"ext_demo", substr, L"0", cnt, MAX_PATH );
					int ret = _wtoi( cnt ) + 1;
					wsprintf( cnt, L"%d", ret );
					//MessageBox( NULL, baidu_cnt, baidu_cnt, MB_OK );
					WriteProfileString( _T("ext_demo") , substr, cnt );
				}

			


			}
		}
	}
	return 0;
}

HRESULT CExtAdfilterObject::OnExtMsg( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch( uMsg )
	{
	case WM_EXTDEMO_NOTIFYSENDDATA:
		{
			TCHAR szData[MAX_PATH] = _T("the current url is %s");
			TCHAR szOutput[ MAX_PATH ] = { 0 };
			_sntprintf( szOutput, SIZEOF( szOutput ), szData, m_szUrl );
			MessageBox( NULL, szOutput, _T("theworld send to extdemo"), MB_OK );
		}
		break;
	}
	return S_OK;
}

HRESULT CExtAdfilterObject::Active( DWORD dwActive )
{
	m_dwActive = dwActive;
	return S_OK;
}

void CExtAdfilterObject::Reset( )
{
}

BOOL CExtAdfilterObject::IsShown( )
{
	return BS_HIDDEN != m_dwActive;
}

// ITWExtMouseMessage
HRESULT CExtAdfilterObject::MouseMessage( MSG* pMsg, BOOL* pbHandled )
{
	return 0;
}


TCHAR* CExtAdfilterObject::GetUrl()
{
	return m_szUrl;
}

HRESULT CExtAdfilterObject::SendExtMessage( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	HRESULT hr = 0;
	if( m_pBrowserHost )
		hr = m_pBrowserHost->SendExtMessage( (DWORD)_lpExtItem_Demo, uMsg, wParam, lParam );

	return hr;
}